﻿using AdminSystem.Applications;
using AdminSystem.Domain.User;
using AdminSystem.Repositories;
using AdminSystem.Write.API.Command.TracebilityCommand;
using AutoMapper;
using MediatR;

namespace AdminSystem.Write.API.Handers.TracebilityHander
{
    public class TracebilityUpdateCommandHandler : IRequestHandler<TracebilityUpdateCommand, ApiResult<object>>
    {
        public TracebilityUpdateCommandHandler(IRepository<Tracebility> repository, IMapper mapper)
        {
            Repository = repository;
            Mapper = mapper;
        }

        public IRepository<Tracebility> Repository { get; }
        public IMapper Mapper { get; }
        /// <summary>
        /// 修改溯源信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<object>> Handle(TracebilityUpdateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<object> result = new ApiResult<object>();
                result.statusCode = StatusCode.Success;
                result.Message = "修改成功";
                var list = Mapper.Map<Tracebility>(request);
                Repository.Update(list);
                return Task.FromResult(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
